package com.daxia.ogmall.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Repository;

import com.daxia.ogmall.daobase.GenericDAOHibernate;
import com.daxia.ogmall.dto.DesignerFanDTO;
import com.daxia.ogmall.model.DesignerFan;
import com.daxia.ogmall.support.Page;

@Repository
public class DesignerFanDAO extends GenericDAOHibernate<DesignerFan> {

    public List<DesignerFan> find(DesignerFanDTO dto, Page page) {
        StringBuilder hql = new StringBuilder();
        List<Object> paras = new ArrayList<Object>();
        hql.append("From DesignerFan n where 1 = 1 ");
        if (dto.getId() != null) {
            hql.append(" and n.id = ? ");
            paras.add(dto.getId());
        }
        if (dto.getDesigner() != null) {
        	if (dto.getDesigner().getId() != null) {
	            hql.append(" and n.designer.id = ? ");
	            paras.add(dto.getDesigner().getId());
        	}
        }			
        if (dto.getFan() != null) {
        	if (dto.getFan().getId() != null) {
	            hql.append(" and n.fan.id = ? ");
	            paras.add(dto.getFan().getId());
        	}
        }			
        return super.find(hql.toString(), paras.toArray(), page);
    }
    
    public DesignerFan findOne(DesignerFanDTO dto) {
		Page page = new Page();
		page.setPageSize(1);
		page.setPage(1);
		
		List<DesignerFan> list = this.find(dto, page);
	    return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    public List<DesignerFan> findByDesignerUserId(Long user_id, Page page) {

        List<Object> paras = new ArrayList<Object>();

        StringBuilder hql = new StringBuilder();
        hql.append("From DesignerFan where designer_user_id = ? ");
        paras.add(user_id);

        return super.find(hql.toString(), paras.toArray(), page);
    }

    public List<DesignerFan> findByFanUserId(Long user_id, Page page) {

        List<Object> paras = new ArrayList<Object>();

        StringBuilder hql = new StringBuilder();
        hql.append("From DesignerFan where fan_user_id = ? ");
        paras.add(user_id);

        return super.find(hql.toString(), paras.toArray(), page);
    }
}
